package com.doyens.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.common.R;
import com.doyens.dto.IntegralDTO;
import com.doyens.entity.Integral;
import com.doyens.entity.Student;
import com.doyens.service.IntegralService;
import com.doyens.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "学生综合积分管理接口")
@Slf4j
@RestController
@RequestMapping("/integral")
public class IntegralController {

    @Autowired
    private IntegralService integralService;

    @Autowired
    private StudentService studentService;

    @GetMapping("/page")
    @ApiOperation(value = "招生老师分页查询方法")
    public R<Page> page(int page, int pageSize) {
        //1.构造分页条件对象
        Page<Integral> pageInfo = new Page<>(page, pageSize);
        Page<IntegralDTO> integralDTOPage = new Page<>(page, pageSize);
        //2.构建查询及排序条件
        LambdaQueryWrapper<Integral> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Integral::getIntegralId);
        //3.执行分页条件查询
        integralService.page(pageInfo, wrapper);
        //4.遍历分页查询列表数据，根据分类ID查询分类信息，从而获取该菜品的分类名称
        //5.拷贝数据，将pageInfo中的数据拷贝到dishDtos中，忽略records字段的数据
        BeanUtils.copyProperties(pageInfo, integralDTOPage, "records");
        List<Integral> records = pageInfo.getRecords();
        List<IntegralDTO> list = records.stream().map((item) -> {
            IntegralDTO integralDTO = new IntegralDTO();
            BeanUtils.copyProperties(item, integralDTO);
            Integer studentId = item.getStudentId();
            Student student = studentService.getById(studentId);
            //判断对象是否存在
            if (student!= null) {
                String sname = student.getSname();
                integralDTO.setSname(sname);
            }
            return integralDTO;
        }).collect(Collectors.toList());
        integralDTOPage.setRecords(list);
        //5.封装数据并返回
        return R.success(integralDTOPage);
    }

    @PostMapping
    @ApiOperation(value = "保存学生费用方法")
    public R<String> save(Integral integral) {
        //接收的学生费用的所有字段
        log.info(integral.toString());
        integralService.save(integral);
        return R.success("添加学生费用成功...");
    }

    @DeleteMapping
    @ApiOperation(value = "删除学生费用方法")
    public R<String> delete(Integer id) {
        log.info("删除的学生费用id:{}", id);
        //执行删除
        integralService.removeById(id);
        return R.success("删除学生费用成功");
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id回显学生费用信息方法")
    public R<IntegralDTO> getOne(@PathVariable Integer id) {
        log.info("回显学生费用id：{}", id);
        IntegralDTO integralDTO = new IntegralDTO();
        Integral integral = integralService.getById(id);
        //赋值数据到dto
        BeanUtils.copyProperties(integral, integralDTO);
        //获取对应学生费用
        Integer studentId = integral.getStudentId();
        Student student = studentService.getById(studentId);
        if (student != null) {
            String sname = student.getSname();
            integralDTO.setSname(sname);
        }
        return R.success(integralDTO);
    }

    @PutMapping
    @ApiOperation(value = "修改学生费用信息方法")
    public R<String> update(Integral integral){
        log.info("修改后的学生费用信息:{}",integral.toString());
        integralService.updateById(integral);
        return R.success("修改学生费用信息成功...");
    }
}
